﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class RCAIUnionActor : RCAIUnionFightBase
    {
        #region 数据
        public InstUFPlayer instPlayer;
        #endregion

        public RCAIUnionActor(RoomCharactor owner)
            : base(owner)
        {
            this.type = AIType.UnionActor;
        }

        public void Init()
        {
            camp = instPlayer.camp;
            Debug.Log("[DEBUG camp] cmap=" + camp );
            InitFSM();
        }
        public void Init( InstUFPlayer instPlayer )
        {
            this.instPlayer = instPlayer;
            Init();
        }

        protected override void BeHit(RoomCharactor srcOwner)
        {
            //owner.PlayAnim(AniName.BEHIT,WrapMode.Once);
        }


        public override void TickHp(RoomCharactor src, float hpDelta)
        {
            if(hpDelta<0)
            {
                BeHit(src);
            }
        }



        #region FSM
        private UnionActorState currState = UnionActorState.None;
        public UnionActorState CurrState
        {
            get { return currState; }
        }
        
        public RCAIUnionActorBase currentState;

        public bool IdleState()
        {
            return CurrState == UnionActorState.Move ||
                CurrState == UnionActorState.Stand;
        }
        
        public bool FightState()
        {
            return CurrState == UnionActorState.Defend ||
                CurrState == UnionActorState.Attack;
        }

        private Dictionary<UnionActorState, RCAIUnionActorBase> allState = new Dictionary<UnionActorState, RCAIUnionActorBase>();
        
        
        public override void Tick()
        {
            base.Tick();
            if (currentState != null)
                currentState.Tick();

        }
        
        private void InitFSM()
        {
            allState[UnionActorState.None] = new RCAIUnionActorBase();
            allState[UnionActorState.Stand] = new RCAIUnionActorStand();
            allState[UnionActorState.Move] = new RCAIUnionActorMove();
            allState[UnionActorState.Attack] = new RCAIUnionActorAttack();
            allState[UnionActorState.Cutting] = new RCAIUnionActorCutting();
            allState[UnionActorState.Mining] = new RCAIUnionActorMining();
            allState[UnionActorState.Revive] = new RCAIUnionActorRevive();
            allState[UnionActorState.Defend] = new RCAIUnionActorDefend();
            allState[UnionActorState.Death] = new RCAIUnionActorDeath();
            allState[UnionActorState.Sing] = new RCAIUnionActorSing();
            allState[UnionActorState.UseItem] = new RCAIUnionActorUseItem();
			allState[UnionActorState.Transfer] = new RCAIUnionActorTransfer();

            foreach(var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionActorState.Stand);
        }

        public override void SwitchState(int newState)
        {
            if((int)currState!=newState)
            {
                allState[currState].Leave();
                currState = (UnionActorState)newState;
                allState[currState].Enter();

                currentState = allState[currState];
            }
        }
        public override void SwitchState(int newState,object param)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionActorState)newState;
                allState[currState].Enter(param);

                currentState = allState[currState];
            }
        }

        #endregion

    }
}